package cz.proks.easyhelpdesk.gwt;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimpleLayoutPanel;
import com.google.web.bindery.event.shared.EventBus;

import cz.proks.easyhelpdesk.gwt.place.DefaultPlace;
import cz.proks.easyhelpdesk.gwt.place.HelpdeskMainPlace;
import cz.proks.easyhelpdesk.gwt.place.HelpdesksPlace;
import cz.proks.easyhelpdesk.gwt.place.LogoutPlace;
import cz.proks.easyhelpdesk.gwt.place.NewHelpdeskPlace;
import cz.proks.easyhelpdesk.gwt.place.NewTicketPlace;
import cz.proks.easyhelpdesk.gwt.place.OAuthPlace;
import cz.proks.easyhelpdesk.gwt.place.SettingsPlace;
import cz.proks.easyhelpdesk.gwt.place.ThreadPlace;
import cz.proks.easyhelpdesk.gwt.place.UserPlace;
import cz.proks.easyhelpdesk.gwt.place.WelcomePlace;
import cz.proks.easyhelpdesk.gwt.ui.widget.ErrorWidget;

public class HelpdeskEntry implements EntryPoint, UncaughtExceptionHandler {
	private final SimpleLayoutPanel panel = new SimpleLayoutPanel();
	private final Place place = new DefaultPlace();
	private final ErrorWidget exceptionWidget = new ErrorWidget();
	
	@Override
	public void onModuleLoad() {	    
	    // For development
	   /* if (!GWT.isProdMode())
	        CookieUtils.setCookieUserId(1l);*/
	    
	    GWT.setUncaughtExceptionHandler(this);	    
	    
		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();
		
		HelpdeskPlaceHistoryMapper placeHistoryMapper = new HelpdeskPlaceHistoryMapper();
        PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(placeHistoryMapper);
        
        registerPlaces(placeHistoryMapper);
        historyHandler.register(placeController, eventBus, place);
		
		ActivityMapper activityMapper = new HelpdeskActivityMapper(clientFactory, placeHistoryMapper);
		ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
		activityManager.setDisplay(panel);		
		
		RootLayoutPanel root = RootLayoutPanel.get();		
		
		root.add(panel);
		
		historyHandler.handleCurrentHistory();				
	}

    @Override
    public void onUncaughtException(Throwable e) {
        exceptionWidget.setException(e);
        panel.setWidget(exceptionWidget);        
    }	

    private void registerPlaces(HelpdeskPlaceHistoryMapper placeMapper) {
        placeMapper.addPlace(new DefaultPlace());
        placeMapper.addPlace(new HelpdeskMainPlace(null));
        placeMapper.addPlace(new HelpdesksPlace());
        placeMapper.addPlace(new LogoutPlace());
        placeMapper.addPlace(new NewHelpdeskPlace());
        placeMapper.addPlace(new NewTicketPlace(null));
        placeMapper.addPlace(new OAuthPlace(null));
        placeMapper.addPlace(new SettingsPlace(null));
        placeMapper.addPlace(new ThreadPlace(null, null));
        placeMapper.addPlace(new WelcomePlace());
        placeMapper.addPlace(new UserPlace(null));
    }       
}
